<?php

namespace Admin\Controller;
class WeiboController extends BaseController {
    public function index(){
        $this->display();
    }
    public function turn(){
        $this->display();
    }
    public function comment(){
        $this->display();
    }
    public function getBlogListByPage(){
        if (!IS_AJAX) {
            $this->error("非法请求");
        }
        $pageIndex = I('post.page');
        $pageSize = I('post.rows');
        $username = I('post.username');
        $content = I('post.content');
        $type = I('post.type','intdiv');
        if($type==0){
            $where['isturn'] = array('EGT',0);
        }else if($type==1){
            $where['isturn'] = array('EQ',0);
        }else{
            $where['isturn'] = array('GT',0);
        }
        if (trim($username)!=''){
            $where['username'] = array('like','%'.$username.'%');
        }
        if (trim($content)!=''){
            $where['content'] = array('like','%'.$content.'%');
        }
        $count =D('WeiboView')->where($where)->count();
        $startno = ($pageIndex - 1) * $pageSize;
        $limit = $startno .',' . $pageSize;
        $blogList = D('WeiboView')->where($where)->limit($limit)->order('time desc')->select();
        //必须将记录总数和记录分别返回
        $result = array(
            "total" => $count,   //记录总数
            "rows" => $blogList  //记录
        );
        $this->ajaxReturn($result,'JSON');
    }
    public function delWeibo () {
        $id = I('post.id', 'intval');
        $uid = I('post.uid', 'intval');
        //删除微博
        if (D('WeiboRelation')->relation(true)->delete($id)) {
            //删除微博中保存的对应转发数据
            M('weibo')->where(array('isturn' => $id))->delete();
            //删除atme中保存的对应数据（转发时候的atme）
            M('atme')->where(array('wid' => $id))->delete();
            //用户发布微博数-1
            M('userinfo')->where(array('uid' => $uid))->setDec('weibo');
            $this->ajaxReturn(array('errorCode'=>0,'msg'=>'操作成功'),'JSON');
        } else {
            $this->ajaxReturn(array('errorCode'=>1,'msg'=>'失败'),'JSON');
        }
    }
    public function getCommentListByPage(){
        if (!IS_AJAX) {
            $this->error("非法请求");
        }
        $pageIndex = I('post.page');
        $pageSize = I('post.rows');
        $content = I('post.content');
        $username = I('post.username');
        if (trim($content)!=''){
            $where['content'] = array('like','%'.$content.'%');
        }else{
            $where['id'] = array('GT',0);
        }
        if (trim($username)!=''){
            $where['username'] = array('like','%'.$username.'%');
        }
        $count =D('CommentView')->where($where)->count();
        $startno = ($pageIndex - 1) * $pageSize;
        $limit = $startno .',' . $pageSize;
        $commentList = D('CommentView')->where($where)->limit($limit)->order('time desc')->select();
        $result = array(
            "total" => $count,
            "rows" => $commentList
        );
        $this->ajaxReturn($result,'JSON');
    }
    public function delComment () {
        $id = I('post.id', 'intval');
        $wid = I('post.wid', 'intval');
        //删除评论
        if (M('comment')->delete($id)) {
            //微博评论数-1
            M('weibo')->where(array('id' => $wid))->setDec('comment');
            $this->ajaxReturn(array('errorCode'=>0,'msg'=>'操作成功'),'JSON');
        } else {
            $this->ajaxReturn(array('errorCode'=>1,'msg'=>'失败'),'JSON');
        }
    }
}
?>